package utils

import (
	//"encoding/json"
	"errors"
	"fmt"
	"reflect"

	"github.com/mitchellh/mapstructure"
)

func ToStruct(input interface{}, output interface{}) error {
	inputType := reflect.TypeOf(input)
	inputTypeString := inputType.Kind().String()
	outputType := reflect.TypeOf(output)
	outputTypeString := outputType.Kind().String()
	outputValue := reflect.ValueOf(output)
	if outputTypeString != "ptr" { //如果不是指针就返回错误
		return errors.New("output no ptr")
	}
	if inputType == outputType { //如果类型相等
		fmt.Println("类型完全相同")
		outputValue.Elem().Set(reflect.ValueOf(input).Elem())
		return nil
	}
	if inputType == outputType.Elem() { //如果类型相等
		fmt.Println("类型的指针相同")
		outputValue.Elem().Set(reflect.ValueOf(input))
		return nil
	}
	if inputTypeString == "map" {
		return mapstructure.Decode(input, output)
	}
	return nil

}
